import type {CheckAuthParams,AuthObjType} from "@@/hw-auth/modal.d"
import {pageAuthChecker} from "@@/hw-auth/components/AuthContext";
import {singleAuth} from '@@/hw-auth/utils/check'


export const checkAuth=({auth,...config}:CheckAuthParams)=>{
  const {checker,...otherConfig}=config;
  const {resultType="bool"}=otherConfig;
  const relChecker=checker||pageAuthChecker;
  const isArray=Array.isArray(auth);
  if (!isArray){
       return  singleAuth(auth,{...otherConfig,checker:relChecker});
  }
  const arrayAuth=(auth as (AuthObjType|string)[]);
  const resultAuth:any[]=[];
  arrayAuth.forEach((item)=>{
    const result=singleAuth(item,{...otherConfig,checker:relChecker});
    if(!result){
        return;
    }
    if (resultType==="bool"){
        resultAuth.push(item);
        return;
    }
    resultAuth.push(result);
  });
  return resultAuth;
}

